package com.charging.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.charging.bean.ChageApiResultData;
import com.charging.bean.Site;
import com.charging.bean.SiteImage;
import com.charging.bean.layuiBean.ResultPage;
import com.charging.bean.layuiBean.VoPage;
import com.charging.config.config.BaseController;
import com.charging.config.result.ApiResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

/**
 * <p>
 * 站点信息 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-16
 */
@Controller
@RequestMapping("admin/site")
public class SiteController extends BaseController {

    /**
     * @return 站点首页
     */
    @RequestMapping(value = "site.html",method = RequestMethod.GET)
    public String site(){
        return "site/site";
    }
    /**
     * @return 站点添加
     */
    @RequestMapping(value = "site_add.html",method = RequestMethod.GET)
    public String site_add(){
        return "site/site_add";
    }

    @RequestMapping(value = "api.html",method = RequestMethod.GET)
    public String api(){
        return "site/api";
    }

    /**
     * @return 站点编辑
     */
    @RequestMapping(value = "site_edit.html",method = RequestMethod.GET)
    public String site_edit(Model model,Long id){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("site_id",id);
        List<SiteImage> siteImage = siteImageService.list(queryWrapper);
        model.addAttribute("url",siteImage);
        return "site/site_edit";
    }
    /**
     * @return 站点地图
     */
    @RequestMapping(value = "charg_map",method = RequestMethod.GET)
    public String charg_map(){
        return "site/charg_map";
    }
    /**
     * @return 添加折扣
     */
    @RequestMapping(value = "api_discount.html",method = RequestMethod.GET)
    public String api_discount(Model model, String ids){
        model.addAttribute("ids",ids);


        return "site/api_discount";
    }



    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Site> selectMyPage(VoPage voPage){
        return siteService.selectMyPage(voPage);
    }

    /**
     * @param site
     * @return 添加
     */
    @RequestMapping(value = "saveSite",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult saveSite(@RequestBody Site site){
        try {
            return  siteService.insertP(site);
        } catch (SQLException e) {
            e.printStackTrace();
            return ApiResult.fail("保存出现意外，有图片丢失。");
        }
    }


    /**
     * @param site
     * @return 修改
     */
    @RequestMapping(value = "updateByIdSite",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult updateByIdSite(@RequestBody Site site){
        try {
            return  siteService.updateSite(site);
        } catch (SQLException e) {
            e.printStackTrace();
            return ApiResult.fail("保存出现意外，有图片丢失。");
        }
    }

    /**
     * @param site
     * @return 删除
     */
    @RequestMapping(value = "deleteByIdSite",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult deleteByIdSite(@RequestBody Site site){
        if(siteService.removeById(site.getId())){
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("site_id",site.getId());
            List<SiteImage> siteImages = siteImageService.list(queryWrapper);
            for (int i = 0; i < siteImages.size(); i++) {
                if(!siteImageService.removeById(siteImages.get(i).getId())){
                    return ApiResult.fail();
                }
            }
        }else{
            return ApiResult.fail("删除失败");
        }
        return ApiResult.ok();
    }


    /**
     * @param voPage
     * @param lng
     * @param lat
     * @return 查询api接口数据
     */
    @RequestMapping(value = "selectApiMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<ChageApiResultData> selectApiMyPage(VoPage voPage, String lng, String lat){
        return siteService.selectApiMyPage(voPage, lng, lat);
    }


}
